/*
* Ajax异步请求comment,评论
* */
function postThis() {
     var blogId  =  $("#blog_id").val();
     var content = $("#comment_content").val();
    commentToComment(blogId,3,content);

}
function commentToComment(targetId,type,content) {
    if (!content){
        alert("不能回复空内容哦~");
        return;
    }
    $.ajax({
        url:"/comment",
        type:"POST",
        data:JSON.stringify({
            "parentId":targetId,
            "content":content,
            "type":type
        }),
        dataType:"json",
        contentType: "application/json;charset=UTF-8",
        success:function (res) {
            if (res.code == 200){
                /* //添加成功，关闭编辑窗口
                 $("#comment_content").hide();*/
                window.location.reload();
            }else {
                if (res.code == 2003){
                    //没有登录
                    if(confirm(res.message)){
                        localStorage.setItem("closeAble","true");
                        window.open("/gitee/auth");
                    }
                }else {
                    alert(res.message);
                }
            }
        }
    });
}
function comment(e) {
    var id = e.getAttribute("dataId");
    var content = $("#input-"+id).val();
    if (!content){
        alert("不能回复空内容哦~");
        return;
    }
    $.ajax({
        url:"/comment/comment",
        type:"POST",
        data:JSON.stringify({
            "parentId":id,
            "content":content,
            "type":4 //博客的回复
        }),
        dataType:"json",
        contentType: "application/json;charset=UTF-8",
        success:function (res) {
            if (res.code == 200){
                /* //添加成功，关闭编辑窗口
                 $("#comment_content").hide();*/
                window.location.reload();
            }else {
                if (res.code == 2003){
                    //没有登录
                    if(confirm(res.message)){
                        localStorage.setItem("closeAble","true");
                        window.open("/gitee/auth");
                    }
                }else {
                    alert(res.message);
                }
            }
        }
    });
}
/*
* 评论的回复
*
* 
* */
function againComment(e) {
    var id = e.getAttribute("data-id");
    var comments = $("#coment-"+id);
    var show = $("#show-"+id);
    //获取二级评论展开状态
    var collapse  = e.getAttribute("data-collapse");
    if (collapse){
        //折叠二级评论
        comments.removeClass("in");
        e.removeAttribute("data-collapse");
        e.classList.remove("active");
    } else {

//展开二级评论
        comments.addClass("in");
        $.ajax({
            url: "/blogCommentAgain/"+id,
            contentType: "application/json;charset=UTF-8",
            dataType: "json",
            async:false,
            type: "get",
            success: function (msg) {
                var div ='';
                for (var i = 0; i < msg.data.length; i++) {
                    var comment = msg.data[i];
                    var newdiv = ' <div class="media">\n' +
                        '                                        <div class="media-left">\n' +
                        '                                            <a href="#">\n' +
                        '                                                <img class="img-circle"\n' +
                        '                                                    src='+comment.user.avatarUrl+'>\n' +
                        '                                            </a>\n' +
                        '                                        </div>\n' +
                        '                                        <div class="media-body" >\n' +
                        '                                            <h4 class="media-heading description">'+comment.user.name+'</h4>\n' +
                        '                                            <div >\n' +
                        '\n' +  comment.content+
                        '                                            </div>\n' +
                        '                                            <div class="menus">\n' +
                        '                                                <span class="pull-right" >'+moment(comment.gmtCreate).format('YYYY年MM月DD日')+'</span>\n' +
                        '                                            </div>\n' +
                        '                                        </div>\n' +
                        '                                    </div>';
                    div += newdiv;
                }
                $(show).html(div);
            }
        })
        //标记二级评论展开状态
        e.setAttribute("data-collapse","in");
        e.classList.add("active");
    }
}
function selectTag(e) {
    var value = e.getAttribute("data-tag");
    var previous =  $("#tag").val();
    if (previous.indexOf(value) == -1) {
        //没有添加过这个标签了
        if (previous){
            //如果输入框有标签值，拼接点击的标签
            $("#tag").val(previous + ',' + value);
        }else {
            $("#tag").val(value);
        }
    }

}
function showTag() {
   $("#select-tag").show();
}